package spring.angularjs.customhandler;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

import spring.angularjs.domain.Userinfo;
import spring.angularjs.services.Services;

@Component
public class MyUserDetailsService implements UserDetailsService {

	@Autowired
	private Services services;
	
	@Override
	public UserDetails loadUserByUsername(String loginId)
			throws UsernameNotFoundException {
		Userinfo user = services.getUserAuthentication(loginId);
		Set<GrantedAuthority> setAuth = new HashSet<GrantedAuthority>();
		setAuth.add(new SimpleGrantedAuthority(user.getRole().getRolename()));
		List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>(setAuth);
		return new User(user.getLoginId(), user.getPassword(), true, true, true, true, authorities);
	}

}
